/*******************************************************************************
 * Copyright (c) 2006 CEA List.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     CEA List - initial API and implementation
 *******************************************************************************/
package com.cea.papyrus.core.editpart;

import java.util.Iterator;

import org.eclipse.draw2d.IFigure;
import org.eclipse.draw2d.geometry.Dimension;
import org.eclipse.draw2d.geometry.Point;
import org.eclipse.draw2d.geometry.Rectangle;
import org.eclipse.emf.common.util.EList;
import org.eclipse.gef.GraphicalEditPart;
import org.eclipse.uml2.uml.OpaqueExpression;

import com.cea.papyrus.core.editor.ModelManager;
import com.cea.papyrus.core.figures.IconFigure;
import com.cea.papyrus.core.figures.NodeFigure;
import com.cea.papyrus.core.figures.NoteFigure;
import com.cea.papyrus.core.helper.ModelerHelper;
import com.cea.papyrus.core.utils.Debug;
import com.cea.papyrus.diagraminterchange2.di2.GraphElement;
import com.cea.papyrus.diagraminterchange2.di2.GraphNode;
import com.cea.papyrus.diagraminterchange2.di2.Property;
import com.cea.papyrus.diagraminterchange2.di2.Uml1SemanticModelBridge;

/**
 * Control class for {@link OpaqueExpression}.
 */
public class OpaqueExpressionEditPart extends GraphNodeWithSemanticEditPart {

	/**
	 * Creates a new OpaqueExpressionEditPart.
	 * @param modelManager model manager in charge of the various models, graphical and domain
	 * @param graphNode graph node controlled by this edit part
	 */
	public OpaqueExpressionEditPart(GraphNode graphNode,ModelManager modelManager){
		super(graphNode, modelManager);
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	protected IFigure createFigure() {
		return new NoteFigure();			
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	protected void refreshVisuals() {
		Debug.debug(this, "refreshvisuals", channel);

		Dimension size = getGraphNode().getSize();
		Point p = getGraphNode().getPosition();
		Rectangle r = new Rectangle(p ,size);

		IFigure fig = getFigure();

		// if fig is a stereotype figure
		if (!(fig instanceof NoteFigure)) {
			IconFigure icf = (IconFigure) getFigure();
			//icf.setName(getUmlElement().getBody());
		} else {
			// Retrieve current class and figure
			NoteFigure f = (NoteFigure) fig; //get Class figure
			f.setComment(setOpaqueExpressionBodies()); //set the content of the figure

			// if fig is a NodeFigure
			((NodeFigure) fig).refreshColors(getGraphNode());
			// set additionnal graphical parameter
			f.setfigureFont(getGraphNode().getFontFamily());
			f.setFontSize((getGraphNode().getFontSize()));
			f.setLineThickness((getGraphNode().getLineThickness()));
		}

		((GraphicalEditPart) getParent()).setLayoutConstraint(this, fig, r);
	}

	/**
	 * {@inheritDoc}
	 */
	public org.eclipse.uml2.uml.Element getUmlElement() {
		return ((Uml1SemanticModelBridge)((GraphElement)getGraphNode().getSemanticParent()).getSemanticModel()).getElement();
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public void activate() {
		if (isActive()) {
			return;
		}
		super.activate();
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public void deactivate() {
		if (!isActive()) {
			return;
		}
		super.deactivate();
	}

//	/*
//	* (non-Javadoc)
//	* @see com.cea.papyrus.core.classdiagram.editpart.CommentEditPart#initLabelControlerManager()
//	*/
//	/**
//	* 
//	*/
//	protected void initLabelControlerManager() {
//	}


//	/*
//	* (non-Javadoc)
//	* @see com.cea.papyrus.core.classdiagram.editpart.CommentEditPart#performDirectEdit(org.eclipse.gef.Request)
//	*/
//	/**
//	* 
//	* 
//	* @param req 
//	*/
//	public void performDirectEdit(Request req) {
//	// no direct editing is allowed here
//	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	protected boolean checkDefaultEdition() {
		return false;
	}

	/**
	 * @return 
	 */
	private String setOpaqueExpressionBodies() {
		String bodies="";

		//get list of opaque expression bodies to display
		EList<Property> graphElementPropList = getGraphNode().getSemanticParent().getProperty();
		Iterator<Property> gePropIter = graphElementPropList.iterator();
		while(gePropIter.hasNext()) {
			Property prop = gePropIter.next();
			if(ModelerHelper.OPAQUE_EXPRESSION_BODY_DISPLAY.equals(prop.getKey())) {
				bodies+=prop.getValue()+"\n\n";
			}
		}
		return bodies;
	}

}
